﻿using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Antu.WebControls
{
    /// <summary>
    /// 水平对齐方式
    /// </summary>
    public enum HorizontalAlignment
    {
        /// <summary>
        /// 左
        /// </summary>
        Left,
        /// <summary>
        /// 中
        /// </summary>
        Center,
        /// <summary>
        /// 右
        /// </summary>
        Right
    }

    /// <summary>
    /// 提示层
    /// </summary>
    [Description("Antu.WebControls.ToolTip")]
    [ToolboxData("<{0}:CustomToolTip runat=server></{0}:CustomToolTip>")]
    public class ControlToolTip : WebControl
    {
        //-------------------------自定义杂项------------------------------------------
        private HorizontalAlignment _TextAlign;

        /// <summary>
        /// 提示层的字体水平对起方式
        /// </summary>
        [
            Description("提示层的字体水平对起方式"),
            Category("自定义外观"),
            DefaultValue(""),
        ]
        public HorizontalAlignment TextAlign
        {
            get { return _TextAlign; }
            set { _TextAlign = value; }
        }

        private string _ToolTipTitle;

        /// <summary>
        /// 控件的提示标题
        /// </summary>
        [
            Description("控件的提示标题"),
            Category("自定义杂项"),
            DefaultValue(""),
        ]
        public string ToolTipTitle
        {
            get { return _ToolTipTitle; }
            set { _ToolTipTitle = value; }
        }

        private string _ToolTipContent;

        /// <summary>
        /// 控件的提示内容
        /// </summary>
        [
            Description("控件的提示内容"),
            Category("自定义杂项"),
            DefaultValue(""),
        ]
        public string ToolTipContent
        {
            get { return _ToolTipContent; }
            set { _ToolTipContent = value; }
        }

        /// <summary>
        /// 显示提示的控件的名称
        /// </summary>
        [
            Description("显示提示的控件的名称"),
            Category("自定义杂项"),
            DefaultValue(""),
        ]
        public string ControlToToolTip
        {
            get
            {
                object o = ViewState["ControlToToolTip"];
                return ((o == null) ? String.Empty : (string) o);
            }
            set { ViewState["ControlToToolTip"] = value; }
        }

        //-------------------------自定义杂项 结束-------------------------------------

        /// <summary>
        /// 根据控件的Id查找控件。
        /// </summary>
        /// <param name="control">需要查找控件的控件（查找控件的起始点）。</param>
        /// <param name="controlId">需要查找的控件Id。</param>
        /// <returns>与Id匹配的控件（未找到返回空）。</returns>
        /// <!--
        /// 创建人  : LiKun
        /// 创建时间: 2006-11-25
        /// -->
        public  Control FindControl(Control control, string controlId)
        {
            Control tempControl = control;
            Control returnControl = null;
            if ((null != control) && (!string.IsNullOrEmpty(controlId)))
            {
                if (control == control.Page)
                {
                    return control.FindControl(controlId);
                }
                while ((returnControl == null) && (tempControl != control.Page))
                {
                    tempControl = tempControl.NamingContainer;
                    if (tempControl == null)
                    {
                        break;
                    }
                    returnControl = tempControl.FindControl(controlId);
                }
            }
            return returnControl;
        }
        /// <summary>
        /// 覆盖 OnLoad 方法
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            WebControl ctrl = FindControl(this, ControlToToolTip) as WebControl;
            if (ctrl != null)
            {
                StringBuilder sb = new StringBuilder();
                if (Width.Value > 0)
                {
                    sb.Append(string.Format("this.T_WIDTH={0};", Width.Value));
                }
                if (!string.IsNullOrEmpty(TextAlign.ToString()))
                {
                    sb.Append(string.Format("this.T_TEXTALIGN='{0}';", TextAlign.ToString()));
                }
                if (!BackColor.IsEmpty)
                {
                    sb.Append(string.Format("this.T_BGCOLOR='{0}';", BackColor.Name));
                }
                if (!BorderColor.IsEmpty)
                {
                    sb.Append(string.Format("this.T_BORDERCOLOR='{0}';", BorderColor.Name));
                }
                if (BorderWidth.Value > 0)
                {
                    sb.Append(string.Format("this.T_BORDERWIDTH={0};", BorderWidth.Value));
                }
                if (!Font.Size.Unit.IsEmpty)
                {
                    sb.Append(string.Format("this.T_FONTSIZE={0};", Font.Size.Unit.Value));
                }
                if (!ForeColor.IsEmpty)
                {
                    sb.Append(string.Format("this.T_FONTCOLOR='{0}';", ForeColor.Name));
                }
                if (!string.IsNullOrEmpty(ToolTipTitle))
                {
                    sb.Append(string.Format("this.T_TITLE='{0}';", ToolTipTitle));
                }
                if (!string.IsNullOrEmpty(_ToolTipContent))
                {
                    ctrl.Attributes.Add("onmouseover", sb.ToString() + "return escape('" + _ToolTipContent + "')");
                }
            }
            // 检查是否脚本是否已经被注册 
            if (!Page.ClientScript.IsStartupScriptRegistered(GetType(), "ToolTipScrip"))
            {
                string toolTipScrip = ReadResourceString("Antu.WebControls.ToolTip.Tooltip.js");
                Page.ClientScript.RegisterStartupScript(GetType(), "ToolTipScrip", toolTipScrip, true);
            }
        }

        /// <summary>
        /// 从当前的指定的资源文件中读入脚本
        /// </summary>
        /// <param name="resourceName">资源文件名称</param>
        /// <returns></returns>
        private string ReadResourceString(string resourceName)
        {
            Assembly dll = Assembly.GetExecutingAssembly();
            StreamReader reader;
            reader = new StreamReader(dll.GetManifestResourceStream(resourceName));
            string resourceString = reader.ReadToEnd();
            reader.Close();

            return resourceString;
        }

        /// <summary>
        /// 输出 html
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
        }
    }
}